package com.qiku.controller;

import com.github.pagehelper.PageInfo;
import com.qiku.domain.Menu;
import com.qiku.domain.MenuVo;
import com.qiku.domain.ResponseResult;
import com.qiku.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询菜单列表
     * @param
     * @return
     */
    @GetMapping("/findAllMenu")
    public ResponseResult findAllMenu() {
        List<Menu> menuList = menuService.findAllMenu();
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",menuList);
        ResponseResult result = new ResponseResult(true, 200, "响应成功~", map);
        return result;
    }

    /**
     * 回显菜单信息(包含父子  菜单信息)
     * @param id
     * @return
     */
    @GetMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(Integer id) {
        if (id == -1){  //
            //添加菜单操作  不需要回显
            List<Menu> menuList = menuService.findSubMenuListByPid(id);
            HashMap<String, Object> map = new HashMap<>();
            map.put("menuInfo" ,null);
            map.put("parentMenuInfo" ,menuList);
            ResponseResult result = new ResponseResult(true, 200, "响应成功~", map);
            return result;
        } else {
            //修改操作  回显当前菜单可以选择的上级菜单
            Menu menu = menuService.findMenuById(id);
            List<Menu> menuList = menuService.findSubMenuListByPid(-1);//可以选择的上级菜单
            HashMap<String, Object> map = new HashMap<>();
            map.put("menuInfo" ,menu);
            map.put("parentMenuInfo" ,menuList);
            ResponseResult result = new ResponseResult(true, 200, "响应成功~", map);
            return result;
        }
    }

    /**
     * 保存和修改菜单
     * @param menu
     * @return
     */
    @PostMapping("/saveOrUpdateMenu")
    public ResponseResult saveOrUpdateMenu(@RequestBody Menu menu) {
        ResponseResult result = null;
        if (null != menu.getId() && 0 < menu.getId()) {
            menuService.updateMenu(menu);
            result = new ResponseResult(true, 200, "修改成功~", null);
        } else {
            menuService.saveMenu(menu);
            result = new ResponseResult(true, 200, "保存成功~", null);
        }
        return result;
    }
}
